Imports System.Data

Partial Class Admin_AR_No
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("AR_Start_btn"), LinkButton)
        Dim img As Image = Master.FindControl("imgAR_Start")
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)

        img.ImageUrl = "~/images/redT.PNG"
        Edit_Button.ForeColor = Drawing.Color.Black
        Edit_Panel.Visible = True

        sdsARType.DataBind()
    End Sub

    Protected Sub DataGrid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles DataGrid.RowCommand
        If e.CommandName = "cmdUpdate" Then
            Dim r As GridViewRow = e.CommandSource.parent.parent

            If Not IsNothing(r) Then
                Dim lblID As Label = r.FindControl("lblID")
                Dim lblARType As Label = r.FindControl("lblARType")
                Dim txtStartNo As TextBox = r.FindControl("txtStartNo")
                Dim lblCurrNo As Label = r.FindControl("lblCurrNo")
                

                'If isValidAR(lblID.Text, Val(txtStartNo.Text)) Then
                Dim dao As New DAOMain.AR_TYPE_MSTR_DAO
                Dim vo As New DAOMain.AR_TYPE_MSTR_VO

                With vo
                    .AR_TYPE_SEQ_CD = lblID.Text
                    .AR_TYPE_DESC = Trim(lblARType.Text)
                    .AR_TYPE_START_NO = txtStartNo.Text
                    .AR_TYPE_CURR_NO = txtStartNo.Text
                    .AR_TYPE_LAST_UPD_BY = Session("UserName")
                    .AR_TYPE_LAST_UPD_DT = Now
                End With

                dao.Update(vo)

                dao = Nothing
                vo = Nothing
                'Else
                '    Dim pFunc As New PolyFunc.ASPFunc
                '    pFunc.MessageAlert(Me, "Invalid AR No...")
                '    pFunc = Nothing
                'End If
            End If

            DataGrid.EditIndex = -1
            sdsARType.DataBind()
        End If
    End Sub

    Private Function isValidAR(ByVal mCd As String, ByVal mVal As Integer) As Boolean
        Dim blnReturn As Boolean = True
        Dim dm As New DAOMain.CommonDAO
        Dim mSql As String = "SELECT B.ar_type_seq_cd, A.payment_no" & _
                            " FROM payment_dtl AS A INNER JOIN" & _
                            " ar_type_payment_dtl AS B ON A.payment_seq_cd = B.payment_seq_cd" & _
                            " Where ar_type_seq_cd = " & dm.Q(mCd) & _
                            " And A.payment_no >= " & mVal

        Dim dt As DataTable = dm.ExecQuery(mSql)

        If dt.Rows.Count > 0 Then
            blnReturn = False
        End If

        Return blnReturn
    End Function
End Class
